package com.quanshu.shop.main.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.quanshu.exchange.R;
import com.quanshu.shop.main.view.ShopMainGoodsFragment;

import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/7/17
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class RecommendGoodsAdapter extends DelegateAdapter.Adapter<RecommendGoodsAdapter.RecommendGoodsViewHolder> {
    private Context mContext;
    private List<ShopMainGoodsFragment> shopMainGoodsFragmentList;
    private LayoutHelper layoutHelper;
    private ShopMainRecommendGoodsPageFragmentAdapter shopMainRecommendGoodsPageFragmentAdapter;
    private int mCurrentIndex = 0;
    public static final int RECOMMEND_GOODS_TYPE = 3;

    public RecommendGoodsAdapter(Context mContext, List<ShopMainGoodsFragment> shopMainGoodsFragmentListInit, LayoutHelper layoutHelper, FragmentManager fragmentManager) {
        this.mContext = mContext;
        this.shopMainGoodsFragmentList = shopMainGoodsFragmentListInit;
        this.layoutHelper = layoutHelper;
        shopMainRecommendGoodsPageFragmentAdapter = new ShopMainRecommendGoodsPageFragmentAdapter(fragmentManager, shopMainGoodsFragmentList);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @NonNull
    @Override
    public RecommendGoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == RECOMMEND_GOODS_TYPE) {
            return new RecommendGoodsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_shop_recommend_goods, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendGoodsViewHolder holder, int position) {
        holder.viewPager.setAdapter(shopMainRecommendGoodsPageFragmentAdapter);
        holder.viewPager.setCurrentItem(mCurrentIndex);
        holder.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                holder.linearLayout.getChildAt(mCurrentIndex).setEnabled(false);
                holder.linearLayout.getChildAt(position).setEnabled(true);
                mCurrentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public int getItemViewType(int position) {
        return RECOMMEND_GOODS_TYPE;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class RecommendGoodsViewHolder extends RecyclerView.ViewHolder {
        ViewPager viewPager;
        LinearLayout linearLayout;

        public RecommendGoodsViewHolder(View itemView) {
            super(itemView);
            viewPager = itemView.findViewById(R.id.item_shop_recommend_vp);
            linearLayout = itemView.findViewById(R.id.item_shop_recommend_ll);
            View view;
            for (int a = 0; a < shopMainGoodsFragmentList.size(); a++) {
                view = new View(mContext);
                view.setBackgroundResource(R.drawable.selector_shop_main_viewpager);
                if (a == 0) {
                    view.setEnabled(true);
                } else {
                    view.setEnabled(false);
                }
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
                if (a != 0) {
                    layoutParams.leftMargin = 20;
                }
                linearLayout.addView(view, layoutParams);
            }
        }

    }
}
